import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index'
//引入ElementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 引用ElementUI
Vue.use(ElementUI)
//关闭提示
Vue.config.productionTip = false

var axios = require('axios')

// 全局注册，之后可在其他组件中通过 this.$axios 发送数据
Vue.prototype.$axios = axios

// 设置反向代理，前端请求默认发送到 http://localhost:8088/api
axios.defaults.baseURL = 'http://localhost:80/api'

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

//钩子函数，访问路由前调用
router.beforeEach((to, from, next) => {
    //路由需要认证
    if (to.meta.requireAuth) {
      //判断store里是否有token
      if (store.state.token) {
        next()
      } else {
        next({
          path: '/',
        })
      }
    } else {
      next()
    }
  }
)
